<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/8/2
 * Time: 17:08
 */
namespace Api\Controller;
use Think\Controller;
use Think\Model;

class MonthController extends BaseController
{
    public function index(){

        $now_month=date('m',time());
        $before_month=$now_month-1;
        $m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上个月的开始日期
        $t = date('t',strtotime($m)); //上个月共多少天

        $start = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上个月的开始日期
        $date=str_replace('-','',$start);
        $date=substr_replace($date,'',6);

        $end = date('Y-m-d', mktime(0,0,0,date('m')-1,$t,date('Y'))); //上个月的结束日期
       // mktime(0,0,0,$before_month)
        $start_time=strtotime($start);
        $end_time=strtotime($end);
        $where['ro.set_time']=array('between',array($start_time,$end_time));
        // 订单总金额 维修金 保证金 利息总金额
        $total=M('repair_order ro')
            ->join('left join zsf_repair_order_protocol p on p.order_id=ro.repair_order_num_alias')
            ->join('left join zsf_ratemanage r on r.order_id=ro.repair_order_num_alias')
            ->where($where)
            ->field('sum(ro.total) total,sum(r.money) rate,sum(p.bond) bond,sum(p.maintain) maintain')
            ->find();
        $rewards=M('rewards_money')->where('money>0')->field('sum(money) money')->find();   //奖励
        $rewards_total=M('rewards_money')->field('sum(money) money')->find();        //佣金总金额

        $data['sum_date']=$date;
        $data['r_ordertotal']=$total['total'];
        $data['master_total']=$rewards_total['money'];
        $data['master_rewards']=$rewards['money'];
        $data['master_signin']=0;
        $data['master_repair']=$total['maintain'];
        $data['master_bond']=$total['bond'];
        $data['repair_interest']=$total['rate'];
        $data['add_time']=time();
        M('monthmoney')->add($data);
        exit;

    }

}